#!/usr/bin/python3
import os
import datetime
import time

if __name__ == '__main__':
    dirpath = os.path.join("E:/8/SGCM_CJ-11_12_v2.22.12.04/", "Logs")  # 要分析处理的目录
    delta = datetime.timedelta(days=1)  # 这里方便测试可以设置成0，也就是把这个文件夹下的所有文件都删除了
    while True:
        now = datetime.datetime.now()  # 获取当前时间
        for root, dirs, files in os.walk(dirpath):
            for file in files:
                pos = file.rfind('.')
                fileExtname = file[pos + 1:]
                # 删除指定后缀名的文件
                if (fileExtname == 'txt'):
                    filePath = os.path.join(root, file)
                    mtime = datetime.datetime.fromtimestamp(os.path.getmtime(filePath))  # 获取文件创建时间
                    if mtime < (now - delta):  # 若创建于delta天前
                        os.remove(filePath)  # 则删掉
                        print('%s remove：%s' % (mtime, filePath))
                else:
                    pass
            break

        time.sleep(3600)
